package kr.hyunist.androidcomplete01saeildamil.ch19;

import kr.hyunist.androidcomplete01saeildamil.*;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class ANREx02 extends Activity {
	boolean bUploading = false;
	final static boolean DEBUG_MODE = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (DEBUG_MODE) {
			StrictMode.ThreadPolicy thPolicy = new StrictMode.ThreadPolicy.Builder()
					.detectAll().penaltyDialog().build();
			StrictMode.setThreadPolicy(thPolicy);
		}
		setContentView(R.layout.anrex);
	}

	public void mOnClick(View v) {
		switch (v.getId()) {
		case R.id.anrexincreasebtn:
			TextView textCounter = (TextView) findViewById(R.id.anrexcountertv);
			int count = Integer.parseInt(textCounter.getText().toString());
			textCounter.setText(Integer.toString(count + 1));
			break;

		case R.id.anrexuploadbtn:
			if (bUploading) {
				return;
			}
			Thread upLoadingThread = new Thread() {

				@Override
				public void run() {
					doUpload();
					mUploadHandler.sendEmptyMessage(0);
				}

			};
			bUploading = true;
			upLoadingThread.start();

			break;
		}
	}

	Handler mUploadHandler = new Handler() {
		public void handleMessage(Message msg) {
			bUploading = false;
			Toast.makeText(ANREx02.this, "업로드 완료!", 0).show();
		}
	};

	private void doUpload() {
		for (int i = 0; i < 100; i++) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
